package com.neusoft.elm.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.SortedMap;

import javax.servlet.jsp.jstl.sql.Result;

import com.neusoft.elm.entity.Business;
import com.sun.corba.se.pept.encoding.OutputObject;

import jdk.nashorn.internal.objects.NativeUint16Array;

public class BusinessDao extends BaseDao {
	
	public List<Business> getAll() {
		List<Business> lbs = null;
		String sql = " SELECT  b.businessAddress,b.businessExplain ,b.businessName , b.deliveryPrice ,"
				+ " b.orderTypeId ,b.remarks , b.starPrice ,b.deliveryPrice  ,b.businessId ,b.imgaddr "
				+ " FROM business b  ";
		Object [] args =null;
		Result  result =  getDate(sql, args);
//		实现离线式数据集到List<Business> 集合的转换
		lbs =   r2o(result);
		return lbs;
		
	}
	
	/**
	 * Result -- > List
	 * @param result
	 * @return
	 */
	private List<Business> r2o(Result result) {
		List<Business> lbs = null;
		if (result == null ||  result.getRowCount() == 0 ) {
			return lbs;
		}
//		
		lbs = new ArrayList<Business>(); 
		SortedMap[]  sms   =  result.getRows();
		for(int index = 0 ;index < result.getRowCount(); index++){
			SortedMap  sm    = sms[index];
//			一个 SortedMap 集合封装了一条记录
			Business business = r2o( sm );
			if (business != null) {
				lbs.add(business);
			}
		}
		return lbs;
	}

	/**
	 * SortedMap -- > Object
	 * @param sm
	 * @return
	 */
	private Business r2o(SortedMap sm) {
		Business business = null;
		if (sm == null || sm.size() == 0 ) {
			return business;
		}
		business = new Business();
//		以下写法是错误
//		business.setBusinessAddress( sm.get("businessAddress") );
//	必须先判空	
		business.setBusinessAddress( sm.get("businessAddress")== null ? "" : sm.get("businessAddress").toString() );
		business.setBusinessExplain(  sm.get("businessExplain") == null ? "" : sm.get("businessExplain").toString() );
		business.setBusinessId( sm.get("businessId") == null ? -1 : Integer.parseInt( sm.get("businessId").toString()  ) );
		business.setBusinessName(  sm.get("businessName") == null ?  "" : sm.get("businessName").toString() );
		business.setDeliveryPrice( sm.get("deliveryPrice") == null ?  -1  : Float.parseFloat(sm.get("deliveryPrice").toString() ) );
		business.setOrderTypeId( sm.get("orderTypeId") == null ? -1 : Integer.parseInt( sm.get("orderTypeId").toString() ) );
		business.setRemarks( sm.get("remarks")== null ? "" : sm.get("remarks").toString() );
		business.setStarPrice( sm.get("starPrice") == null ? -1 : Float.parseFloat(  sm.get("starPrice").toString() ) );
		business.setImgaddr( sm.get("imgaddr") == null ? "sj01.png" :sm.get("imgaddr").toString() );
		
		return business;
	}


	/**
	 * 向数据库插入新的Business对象，返回影响记录的行数
	 * @param business
	 * @return
	 */
	public int  postBusiness(Business business ) {
		int  i =-1;
		String sql = "  INSERT INTO `business` ( businessName, businessAddress, businessExplain, orderTypeId , starPrice, "
				+ "deliveryPrice ,  remarks  ) VALUES (  ? , ? , ? , ? ,  ?  , ? , ?  )  " ;
		Object [] args ={ business.getBusinessName() , business.getBusinessAddress() , business.getBusinessExplain() , 
				business.getOrderTypeId() , business.getStarPrice(), business.getDeliveryPrice() , business.getRemarks()};
		i= modify(sql, args);
		return  i ;
	}
	/**
	 * 
	 * @param business
	 * @return
	 */
	public int modLabelById( Business business  ) {
		int  i =-1;
		String sql = " UPDATE  business b  SET b.businessName  =   ?    WHERE b.businessId = ?      " ;
		Object [] args ={business.getBusinessName(), business.getBusinessId()};
		i= modify(sql, args);
		return  i ;
	}
}




